package com.rent.product.controller;

import java.util.*;

import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.rent.product.entity.PmsCategoryEntity;
import com.rent.product.service.PmsCategoryService;
import com.rent.product.utils.PageUtils;
import com.rent.product.utils.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 商品三级分类
 *
 * @author chan
 * @email chan_2020@gmail.com
 * @date 2021-01-30 17:17:26
 */
@RestController
@RequestMapping("product/pmscategory")

//@Controller
public class PmsCategoryController {
    @Autowired
    private PmsCategoryService pmsCategoryService;

    /**
     * 查询出所有分类以及子分类，以树型结构组装起来
     */
    @RequestMapping("/list/tree")
    @ResponseBody
    public R list() {
        List<PmsCategoryEntity> entities = pmsCategoryService.listWithTree();
        return R.ok().put("data", entities);
    }

    /*
     * 查询所有
     * */
    @ResponseBody
    @RequestMapping(value = "/getTree", produces = "html/text;charset=UTF-8")
    public String getTree(HttpServletResponse response) {
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        //treeTable取所有数据方法
        System.out.println("kk");
        List<PmsCategoryEntity> entities = pmsCategoryService.listAll();
        //转换为json数据
        JSONObject result = new JSONObject();
        result.put("msg", "success");
        result.put("status", 0);
        result.put("data", entities);
        System.out.println(result.toJSONString());
        return result.toJSONString();
    }

    /*
     * 查询数据库id最大的记录
     * */
    @ResponseBody
    @RequestMapping(value = "/getLastList", produces = "html/text;charset=UTF-8")
    public String getLastList(HttpServletResponse response) {
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        //从数据库取出最后一条数据
        List<PmsCategoryEntity> lastList = pmsCategoryService.findLastId();
        //转换为json数据
        JSONObject result = new JSONObject();
        result.put("data", lastList);
        System.out.println(result.toJSONString());
        return result.toJSONString();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save01(@RequestBody PmsCategoryEntity pmsCategory) {
        try {
            //System.out.println(pmsCategory);
            pmsCategoryService.save(pmsCategory);
            return R.ok("数据保存成功");
        } catch (Exception e) {
            return R.error(500,"数据保存失败");
        }
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public R update(@RequestBody PmsCategoryEntity pmsCategory) {
        try {
            pmsCategoryService.updateById(pmsCategory);
            //System.out.println(pmsCategory);
            return R.ok("数据修改成功");
        } catch (Exception e) {
            return R.error(500,"数据修改失败");
        }
    }

    /**
     * 单个删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public R delete(String id) {
        try {
            //System.out.println(id);
            pmsCategoryService.deleteById(id);
            return R.ok("数据删除成功");
        } catch (Exception e) {
            return R.error(500,"数据删除失败");
        }
    }

    /*
     * 批量删除
     * */
    @RequestMapping("/deleteNum")
    @ResponseBody
    public R deleteNum(String isStr) {
        if (isStr != null && !isStr.equals("")) {
            String[] ids = isStr.split(",");
            List<String> aslist = Arrays.asList(ids);
            //System.out.println(aslist);
            pmsCategoryService.removeMenuByIds(aslist);
        }
        return R.ok("数据删除成功");
    }



    @RequestMapping("/PmsCategory")
    public String PmsCategory() {
        return "PmsCategory";

    }


}
